package com.ingrun.tuboshu.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.ingrun.tuboshu.entity.SysAdmin;
import com.ingrun.tuboshu.entity.SysUser;
import com.ingrun.tuboshu.entity.vo.PasswordLoginQ;
import com.ingrun.tuboshu.entity.vo.UpdPasswordQ;
import com.ingrun.tuboshu.entity.vo.UpdUserQ;
import com.ingrun.tuboshu.entity.vo.UserListQ;
import com.ingrun.tuboshu.mapper.SysUserMapper;
import com.ingrun.tuboshu.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ingrun.tuboshu.utils.*;
import com.ingrun.tuboshu.utils.params.PageHelperList;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * <p>
 * 用户表	 服务实现类
 * </p>
 *
 * @author ingrun
 * @since 2025-01-23
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {


    @Override
    public SysUser selectUserByUserName(String username) {
        return this.getOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUsername, username));
    }

    @Override
    public Boolean register(SysUser sysUser) {
        sysUser.setPassword(PasswordUtil.encryptPassword(sysUser.getUsername(), sysUser.getPassword()));
        sysUser.setStatus("1");
        sysUser.setDelFlag("0");
        return this.save(sysUser);
    }

    @Override
    public Boolean updateSelfUserInfo(UpdUserQ query) {
        String loginId = StpKit.USER.getLoginIdAsString();

        if (!loginId.equals(query.getUserId())) {
            throw new ResponseException(ResponseEnum.REQUEST_PARAM_ERROR, "不能修改其他人的用户信息");
        }

        SysUser dbUser = this.getById(query.getUserId());
        if (dbUser == null) {
            throw new ResponseException(ResponseEnum.REQUEST_PARAM_ERROR, "用户不存在");
        }

        SysUser sysUser = new SysUser();
        sysUser.setNickname(query.getNickname());
        sysUser.setPhoneNumber(query.getPhoneNumber());
        sysUser.setEmail(query.getEmail());
        sysUser.setSex(query.getSex());
        sysUser.setAvatar(query.getAvatar());
        sysUser.setStatus(query.getStatus());
        sysUser.setUpdateBy(loginId);

        return this.updateById(sysUser);
    }

    @Override
    public PageHelperList<SysUser> userList(UserListQ query) {
        if (query == null || query.getPage() <= 0 || query.getPageSize() <= 0) {
            return new PageHelperList<>(Collections.emptyList());
        }

        LambdaQueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>().lambda()
                .like(StrUtil.isNotEmpty(query.getNickname()), SysUser::getNickname, query.getNickname())
                .like(StrUtil.isNotEmpty(query.getUsername()), SysUser::getUsername, query.getUsername())
                .like(StrUtil.isNotEmpty(query.getPhoneNumber()), SysUser::getPhoneNumber, query.getPhoneNumber())
                .like(StrUtil.isNotEmpty(query.getEmail()), SysUser::getEmail, query.getEmail())
                .eq(StrUtil.isNotEmpty(query.getStatus()), SysUser::getStatus, query.getStatus())
                .eq(StrUtil.isNotEmpty(query.getSex()), SysUser::getSex, query.getSex())
                .select(SysUser.class, i -> !i.getColumn().equals("password"));  // 查询去除password字段

        PageHelper.startPage(query.getPage(), query.getPageSize());
        return new PageHelperList<>(this.list(queryWrapper));
    }

    @Override
    public Boolean adminResetPassword(UpdPasswordQ query) {
        SysUser dbUser = this.getById(query.getId());
        if (dbUser == null) {
            throw new ResponseException(ResponseEnum.USERNAME_OR_PASSWORD_ERROR, "用户不存在");
        }

        SysUser sysUser = new SysUser();
        sysUser.setId(query.getId());
        sysUser.setPassword(PasswordUtil.encryptPassword(dbUser.getUsername(), query.getNewPassword()));
        return this.updateById(sysUser);
    }

}
